package org.ala.distributed_transaction_commons;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分布式事务中的角色
 *
 * @author 骆毅(ala)
 * @date 2024年2月19日
 */
public enum DistributedTransactionRole {

	COORDINATOR(1, "coordinator", "协调者（既是协调，也是参与）"),
	
	PARTICIPANT(2, "participant", "参与者")
	;
	
	private int val;
	private String code;
	private String desc;
	private DistributedTransactionRole(int val, String code, String desc) {
		this.val = val;
		this.code = code;
		this.desc = desc;
	}
	
	
	static Map<String, DistributedTransactionRole> codeMapper = null;
	static Object LOCK_CODE_MAPPER = new Object();
	/**
	 * 根据val查找
	 */
	public static DistributedTransactionRole getInstance(String code) {
		if (codeMapper == null) {
			synchronized (LOCK_CODE_MAPPER) {
				if (codeMapper == null) {
					codeMapper = Arrays.stream(DistributedTransactionRole.values()).collect(Collectors.toMap(DistributedTransactionRole::getCode, o->o, (o,n)->n));
				}
			}
		}
		return codeMapper.get(code);
	}
	
	
	public int getVal() {
		return val;
	}
	public void setVal(int val) {
		this.val = val;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	
}
